package com.ruoyi.web.controller.system;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.SmsConst;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.SmsLoginDto;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.utils.SmsUtil;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/system/sms")
public class SmsController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private SysLoginService loginService;
    @ApiOperation("发送短信验证码")
    @PostMapping("/sendSmsCode/{phoneNumber}")
    public AjaxResult sendSmsCode(@PathVariable("phoneNumber") String phoneNumber){
        if (!PhoneUtil.isPhone(phoneNumber)){
            throw new ServiceException("对不起，请输入正确的手机号！");
        }
        // 手机号码
        phoneNumber = phoneNumber.trim();
        // 系统的没有的手机号，自动注册，下面这段代码就不需要校验了。
        // 校验手机号
//        SysUser user = sysUserService.selectUserByPhonenumber(phoneNumber);
//        if (StringUtils.isNull(user)) {
//            throw new ServiceException("登录用户：" + phoneNumber+ " 不存在");
//        }else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
//            throw new ServiceException("对不起，您的账号：" + phoneNumber+ " 已被删除");
//        }else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
//            throw new ServiceException("对不起，您的账号：" + phoneNumber+ " 已停用");
//        }

        // 生成短信验证码
        String smsCode = RandomUtil.randomNumbers(6);

        SmsUtil.send(phoneNumber, smsCode);

        // 保存redis缓存
        String uuid = IdUtils.simpleUUID();
        String verifyKey = SmsConst.REDIS_KEY_SMS_LOGIN + uuid;
        redisCache.setCacheObject(verifyKey, smsCode, SmsConst.REDIS_TIMEOUT_SMS_LOGIN, TimeUnit.MINUTES);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("uuid", uuid);
        return ajax;

    }
    @ApiOperation("短信验证码登录验证")
    @PostMapping("/smsLogin")
    public AjaxResult smsLogin(@RequestBody @Validated SmsLoginDto dto){

        AjaxResult ajax = AjaxResult.success();
        // 返回token给前端
        String token = loginService.smsLogin(dto.getPhoneNumber(), dto.getCode(), dto.getUuid());
        ajax.put(Constants.TOKEN, token);

        return ajax;
    }

}
